import sun.reflect.generics.tree.Tree;

import java.util.*;

import static sun.swing.MenuItemLayoutHelper.max;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28956
 * Date: 2024-07-20
 * Time: 20:56
 */
public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val){
            this.val = val;
        }
    }


    /**
     *创建一颗二叉树 创建成功后返回根节点
     * @return
     */
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    //前序遍历 根左右
    void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * 把前序遍历的结果存储到List当中
     * @param root
     * @return
     */
//        List<Integer> ret = new ArrayList<>();
//    public List<Integer> preorderTraversal(TreeNode root) {
//        if(root == null){
//            return null;
//        }
//        ret.add(root.val);
//        preorderTraversal(root.left);
//        preorderTraversal(root.right);
//        return ret;
//    }

    /* public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        list.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        list.addAll(rightTree);
        return list;
    } */

    //前序遍历非递归
    void preOrderNor(TreeNode root){
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    //中序遍历 左根右
    void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //中序遍历非递归
    void inOrderNor(TreeNode root){
        if(root ==null){
            return;
        }
        Stack<TreeNode> stack =new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val+" ");
            cur = top.right;

        }
    }

    /*
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        List<Integer> leftTree = inorderTraversal(root.left);
        list.addAll(leftTree);
        list.add(root.val);
        List<Integer> rightTree = inorderTraversal(root.right);
        list.addAll(rightTree);
        return list;
    }
   */

    //后序遍历 左右根
    void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    //后序遍历非递归
    void postOderNor(TreeNode root){
        TreeNode prev =null;
        if(root ==null){
            return;
        }
        Stack<TreeNode> stack =new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();


            if(top.right == null || top.right == prev){
                System.out.print(top.val+" ");
                stack.pop();
                prev = top;
            }else{
                cur = top.right;
            }


        }
    }

    /*
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        List<Integer> leftTree = postorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree = postorderTraversal(root.right);
        list.addAll(rightTree);
        list.add(root.val);
        return list;
    }
*/


    // 获取树中节点的个数
    public int nodeSize;//成员变量初始化默认0
    int size1(TreeNode root){
        if(root == null){
            return 0;
        }
        nodeSize++;
        size1(root.left);
        size1(root.right);
        return nodeSize;
    }

    //子问题思路：整棵树的节点个数=左子树的节点个数+右子树的节点个数
    int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }

    // 获取叶子节点的个数
    public int leafSize;
    int getLeafNodeCount1(TreeNode root){
        if(root == null){
            return 0;
        }

        if(root.left == null && root.right == null){
            leafSize ++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leafSize;



    }
    //子问题思路：整棵树的节点个数=左子树的节点个数+右子树的节点个数
    int getLeafNodeCount2(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
           return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;

        //递归太多次了,容易超时
        // return getHeight(root.left)>getHeight(root.right) ? getHeight(root.left)+1 :getHeight(root.right)+1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if(ret1 != null){
            return ret1;//在左边找到了，不用去右边
        }

        TreeNode ret2 = find(root.right,val);
        if(ret2 != null){
            return ret2;
        }
        //左右都没有找到
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        if(root == null ){
            return true;
        }
        while (!queue.isEmpty()){
           TreeNode cur = queue.poll();
           if(cur != null){
               queue.offer(cur.left);
               queue.offer(cur.right);
           }else{
               break;//结束当前这个循环
           }
        }
        //需要判断队列中是否有非空元素
        while(!queue.isEmpty()){
            //通过元素逐个出队来判断是不是空
            TreeNode tmp = queue.peek();
            if(tmp == null){
                queue.poll();
            }else{
                return false;
            }
        }
        return true;
    }

}
